package com.banshuibao.bsb_android.http;

import android.text.TextUtils;

import com.banshuibao.bsb_android.MyApplication;
import com.zx.uploadlibrary.helper.ProgressHelper;
import com.zx.uploadlibrary.listener.impl.UIProgressListener;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by zyh on 2017/4/17.
 */

class OkHttpDownload {

    private String url;

    private UIProgressListener listener;

    private Callback callback;

    OkHttpDownload(String url, UIProgressListener listener, Callback callback) {
        this.url = url;
        this.listener = listener;
        this.callback = callback;
    }

    void download() {
        ProgressHelper.addProgressResponseListener(OkHttpClientUtil.getInstance().getClient(), listener, "").newCall(getRequest(url)).enqueue(callback);
    }

    private String getCookieID() {
        String cookieId = MyApplication.applicationInstance.getCookieId();
        return TextUtils.isEmpty(cookieId) ? "" : cookieId;
    }

    private Request getRequest(String downloadUrl) {
        okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
        builder.header("cookieId", getCookieID());
        builder.url(downloadUrl).tag(downloadUrl).post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"), ""));
        return builder.build();
    }

}
